package com.woniu.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.woniu.utils.BaseController;
import com.woniu.entity.Auctiongoods;
import com.woniu.entity.Goods;
import com.woniu.entity.Order;
import com.woniu.entity.Record;
import com.woniu.service.OrderService;
import com.woniu.utils.MyPage;
import com.woniu.vo.OrderDataVo;

@Controller
@RequestMapping("/auction/Order")
public class OrderController extends BaseController {
	@Autowired
	private OrderService orderService;
	
	@RequestMapping("/dealamountchart")
	@ResponseBody
	public void getDealAmountByDate(HttpServletResponse resp) throws IOException {
		resp.setCharacterEncoding("UTF-8");
		List<Map> list = new ArrayList<Map>();
		list = orderService.getDealAmountByDate();
		System.out.println("chart1的list：" + list);
		String json = JSON.toJSONString(list);
		System.out.println("chart1转换json：" + json);
		resp.getWriter().write(json);
	}

	@RequestMapping("/ordercountchart")
	@ResponseBody
	public void getOrderCountByDate(HttpServletResponse resp) throws IOException {
		resp.setCharacterEncoding("UTF-8");
		List<Map> list = new ArrayList<Map>();
		list = orderService.getOrderCountByDate();
		System.out.println("chart1的list：" + list);
		String json = JSON.toJSONString(list);
		System.out.println("chart1转换json：" + json);
		resp.getWriter().write(json);
	}

	// 王世嘉：订单管理页面
	@RequestMapping("/selectByCond")
	@ResponseBody
	public MyPage<OrderDataVo> selectByCond(String bname, String sname, String date1, int pageNum, int pageSize,
			int timeChoose, int moneyChoose) throws Exception {
		return orderService.selectByCond(bname, sname, date1, pageNum, pageSize, timeChoose, moneyChoose);
	}

	@RequestMapping("/deleteOrderByOid")
	public void deleteOrderByOid(Integer oid, HttpServletResponse resp) throws IOException {
		String result = orderService.deleteOrderByOid(oid) + "";
		resp.getWriter().write(result);
	}
	
	@Scheduled(fixedDelay = 6000000)
	@Async
	public void insertOrder() {
		List<Auctiongoods> auctiongoods = orderService.selectAuctiongoods();
		if(auctiongoods.size()!=0) {
			for(Auctiongoods t : auctiongoods) {
				System.out.println(t.getAuctionstatus());
				Record record = orderService.selectRecord(t.getGid());
				if("竞价成功".equals(t.getAuctionstatus())) {
					Integer uBid = record.getuBid();
					Integer uSid = record.getuSid();
					Integer getid = orderService.selectGetidByUsid(uSid);
					double dealPrice = record.getRecordprice();
					double commision = orderService.selectDepositByAgid(t.getAgid());
					double orderTotalPrice = dealPrice+commision;
					Date dealTime = t.getEndingtime();
					Integer gid = t.getGid();
					int buyCount = 1;
					String orderStatus = "0";
					String paymentChannel = "支付宝";
//					orderService.updateGoodstatus(gid);
					orderService.insertOrder(uBid,uSid,getid,dealPrice,commision,orderTotalPrice,dealTime,gid,buyCount,orderStatus,paymentChannel);
				}
			}
		}	
	}



}